//
//  RHApplicationListController.m
//  RuiHeAppApp
//
//  Created by Jun on 2023/7/25.
//

#import "RHApplicationListController.h"
#import "RHApplicationListCell.h"
#import "RHExamineDetailVC.h"
#import "RHApplicationDetailModel.h"
@interface RHApplicationListController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong) UITableView *tableView;

@property(nonatomic ,strong)NSArray *applicationListArr;
@end

@implementation RHApplicationListController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:CFTabViewBgColor];
    [self getMyApplicationList];
    [self setupUI];
}
//我的申请
-(void)getMyApplicationList
{
    [QMUITips showLoadingInView:self.view];
    [RHApplicationDetailModel startGetWithCompletionAPIKey:H_myApplicationList argument:@{} requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        
        if(self.index == 0){
            self.applicationListArr = resultModel.data;
        }else if (self.index == 1){
            NSMutableArray *mtbArr = [NSMutableArray array];
            for (RHApplicationDetailModel *model in resultModel.data) {
                if(model.auditStatus == AuditStatusApproved){
                    [mtbArr addObject:model];
                }
            }
            self.applicationListArr = mtbArr;
        }else if (self.index == 2){
            NSMutableArray *mtbArr = [NSMutableArray array];
            for (RHApplicationDetailModel *model in resultModel.data) {
                if(model.auditStatus == AuditStatusUnaudited){
                    [mtbArr addObject:model];
                }
            }
            self.applicationListArr = mtbArr;
        }else if (self.index == 3){
            NSMutableArray *mtbArr = [NSMutableArray array];
            for (RHApplicationDetailModel *model in resultModel.data) {
                if(model.auditStatus == AuditStatusReject){
                    [mtbArr addObject:model];
                }
            }
            self.applicationListArr = mtbArr;
        }
        [self.tableView reloadData];
        [self.tableView endHeaderRefresh];
    } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        
    }];
}

- (void)setupUI{
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [_tableView setBackgroundColor:UIColor.clearColor];
    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
    [_tableView registerClass:[RHApplicationListCell class] forCellReuseIdentifier:@"RHApplicationListCell"];
    [self.view addSubview:_tableView];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
    }];
    kWeakSelf;
    [_tableView addHeaderRefresh:^{
        [weakSelf getMyApplicationList];
    }];
}


#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.applicationListArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RHApplicationListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RHApplicationListCell"];
    cell.model = self.applicationListArr[indexPath.row];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    RHExamineDetailVC *VC = [[RHExamineDetailVC alloc]init];
    RHApplicationDetailModel *model = self.applicationListArr[indexPath.row];
    VC.Id = model.audit_id;
    VC.title = @"审核详情".rh_localized;
    [self.navigationController pushViewController:VC animated:YES];
}


#pragma mark - JXCategoryListContentViewDelegate
/**
 实现 <JXCategoryListContentViewDelegate> 协议方法，返回该视图控制器所拥有的「视图」
 */
- (UIView *)listView {
    return self.view;
}

@end
